from django.shortcuts import HttpResponse
from app_user.models import Goods
from django.conf import settings
from django.core.mail import send_mail
from app_user.tasks import *
from itsdangerous import URLSafeTimedSerializer

def test(request):
    """测试"""
    token = get_token(1)
    emails = "3375236591@qq.com"
    print(token)
    send_register_active_email.delay(emails, 1, token)

    return HttpResponse("测试")



def get_token(user_id):
    """生成token"""
    serializer = URLSafeTimedSerializer(settings.SECRET_KEY)
    token = serializer.dumps(user_id, salt=settings.SALT)
    return token



def verify_token(token, max_age=3600 * 24):
    """验证token"""
    serializer = URLSafeTimedSerializer(settings.SECRET_KEY)
    try:
        user_id = serializer.loads(token, salt=settings.SALT, max_age=max_age)

    except:
        return None
    return user_id


from django.views.decorators.cache import cache_page


@cache_page(60 * 5)
def my_view(request):
    """测试缓存"""
    print("hhhhhhhhhhhhhhhhhhhhhh")
    return HttpResponse("Hello, world!")


from django.core.cache import cache


def get_data(request):
    """缓存数据库查询结果"""
    key = "my_data_key"
    data = cache.get(key)
    if data is None:
        print("开始缓存")
        data_obj = Goods.objects.filter(id=5).first()
        if not data_obj is None:
            data = data_obj.title
            cache.set(key, data, 60 * 10)
        else:
            return HttpResponse("没有数据")
    return HttpResponse(data)